package com.xudu.qdadabackend.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xudu.qdadabackend.annotation.AuthCheck;
import com.xudu.qdadabackend.common.DeleteRequest;
import com.xudu.qdadabackend.common.ErrorCode;
import com.xudu.qdadabackend.common.Result;
import com.xudu.qdadabackend.constant.UserConstant;
import com.xudu.qdadabackend.model.dto.user.LoginRequest;
import com.xudu.qdadabackend.model.dto.user.RegisterRequest;
import com.xudu.qdadabackend.model.dto.user.SearchUserRequest;
import com.xudu.qdadabackend.model.dto.user.UpdateUserRequest;
import com.xudu.qdadabackend.model.vo.UserVO;
import com.xudu.qdadabackend.service.UserService;
import com.xudu.qdadabackend.utils.GetRequestUtil;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import static com.xudu.qdadabackend.constant.UserConstant.USER_LOGIN_STATE;

/**
 * @className: UserControllerImpl
 * @description: TODO
 * @author: xudu
 * @create: 2024-10-10
 */
@RestController
@RequestMapping("/user")
@Slf4j
public class UserController{

    @Resource
    private UserService userService;

    /**
     * 测试接口
     */
    @ApiOperation("测试接口")
    @GetMapping("/hello")
    public Result hello() {
        return Result.success(10);
    }

    /**
     * 登陆接口
     */
    @ApiOperation("登陆接口")
    @PostMapping("/login")
    public Result login(@RequestBody LoginRequest loginRequest) throws Exception {
        UserVO userVO = userService.LoginUser(loginRequest.getUserAccount(), loginRequest.getUserPassword());
        return Result.success(userVO);
    }

    /**
     * 注册接口
     */
    @ApiOperation("注册接口")
    @PostMapping("/register")
    public Result register(@RequestBody RegisterRequest registerRequest) throws Exception {
        Long id = userService.RegisterUser(registerRequest.getUserAccount(), registerRequest.getUserPassword(), registerRequest.getCheckPassword());
        return Result.success("注册成功！您的id为：" + id);
    }

    /**
     * 退出接口
     */
    @ApiOperation("退出接口")
    @PostMapping("/logout")
    public Result logout() {
        // 移除登录态
        HttpServletRequest request = GetRequestUtil.getRequest();
        request.getSession().removeAttribute(USER_LOGIN_STATE);
        return Result.success("退出成功！");
    }


    /**
     * 分页查询用户信息接口
     */
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    @ApiOperation("分页查询用户信息接口")
    @GetMapping("search/list/page")
    public Result searchListPage(SearchUserRequest searchUserRequest) {
        Page<UserVO> userVOPage = userService.getUserListByPage(searchUserRequest);
        return Result.success(userVOPage);
    }


    /**
     * 更新用户接口
     */
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    @ApiOperation("更新用户接口")
    @PostMapping("/update")
    public Result updateUser(UpdateUserRequest updateUserRequest) {
        Boolean b = userService.updateUser(updateUserRequest);
        return b ? Result.success("更新成功！") : Result.error(ErrorCode.OPERATION_ERROR);
    }

    /**
     * 获取当前登陆用户信息接口
     */
    @ApiOperation("获取当前登陆用户信息接口")
    @GetMapping("/get/current")
    public Result getCurrentUser() {
        UserVO loginUser = userService.getLoginUser();
        return Result.success(loginUser);
    }


    /**
     * 删除用户接口
     */
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    @ApiOperation("删除用户接口")
    @PostMapping("/delete")
    public Result deleteUser(@RequestBody DeleteRequest deleteRequest) {
        Long id = deleteRequest.getId();
        Integer i = userService.deleteUser(id);
        return i > 0 ? Result.success(i) : Result.error(ErrorCode.OPERATION_ERROR);
    }
}
